﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Dapper;
using Dapper.Contrib.Extensions;

namespace ConsoleApp.Dapper
{
    public interface IYearRepository : IRepository<Year>
    {

    }

    class YearRepository : DBRepository<Year>, IYearRepository
    {
        public YearRepository(IDbConnection dbconn)
            : base(dbconn)
        {

        }

        public override Year GetById(int id)
        {
            string sql = "select * from year where AutoId = " + id;
            return _dbconn.Query<Year>(sql).FirstOrDefault();
        }

        public override List<Year> GetAll()
        {
            return _dbconn.Query<Year>("select * from year").ToList();
        }

        public override void Add(Year item)
        {
            string sql = string.Format(@"INSERT INTO year (AutoId,YearName) VALUES({0},""{1}"");SELECT @@identity;", item.AutoId, item.YearName);
            int i = _dbconn.Query<int>(sql).FirstOrDefault();
            item.AutoId = i;

            throw new Exception("just for test not to commit");
        }
    }
}